package com.jiaquyun.jcyx.app

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.jiaquyun.jcyx.entity.CartGoods
import java.lang.ref.WeakReference

object AppConstance {

    val cartListLiveData = MutableLiveData<MutableList<CartGoods>>()


    const val SPS_KEY_TOKEN = "SPS_KEY_TOKEN"
    const val SPS_KEY_USER_INFO = "SPS_KEY_USER_INFO"
    const val SPS_KEY_USER_INDEX = "SPS_KEY_USER_INDEX"
    const val SPS_KEY_USER_SHOPS = "SPS_KEY_USER_SHOPS"

    const val KEY_NAME_PROTOCOL = "KEY_NAME_PROTOCOL"

}

class SafeMutableLiveData<T> : MutableLiveData<T>() {

    private var lastLifecycleOwner: WeakReference<LifecycleOwner>? = null

    fun safeObserve(owner: LifecycleOwner, observer: Observer<T>) {
        lastLifecycleOwner?.get()?.let {
            removeObservers(it)
        }
        lastLifecycleOwner = WeakReference(owner)
        observe(owner, observer)
    }

}


const val KEY_ADDRESS_LIST_ACTION = "KEY_ADDRESS_LIST_ACTION"
const val KEY_ADDRESS_EDIT = "KEY_ADDRESS_EDIT"